

// 重复代码抽取
const postRequest = function (url, data, callback) {
    $.ajax({
        url: url, //要请求的后端地址
        type: "POST", //数据发送的方式(POST或者GET)
        contentType: "application/json",
        dataType: "json", //后端返回的数据格式
        data: JSON.stringify(data), //需要传递的参数
        success: function (result) {
            if (result.code == 401) {
                window.vue.$message({
                    message: result.msg,
                    type: 'warn'
                });
            } else if (result.code == 500) {
                window.vue.$confirm('登录状态已过期，请重新登录！', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '',
                    showCancelButton: false,
                    closeOnClickModal: false,
                    closeOnPressEscape: false,
                    closeOnHashChange: false,
                    lockScroll: true,
                    type: 'warning',
                    showClose: false,
                }).then(() => {
                    window.location.href = "/login";
                });
            } else {
                callback(result);
            }
        },
        error: function () {
            window.vue.$message({
                message: "请检查网络~",
                type: 'warn'
            });
        }
    });
}

const getRequest = function (url, callback) {
    $.ajax({
        url: url, //要请求的后端地址
        type: "GET", //数据发送的方式(POST或者GET)
        success: function (result) {
            if (result.code != 0) {
                window.vue.$confirm('登录状态已过期，请重新登录！', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '',
                    showCancelButton: false,
                    closeOnClickModal: false,
                    closeOnPressEscape: false,
                    closeOnHashChange: false,
                    lockScroll: true,
                    type: 'warning',
                    showClose: false,
                }).then(() => {
                    window.location.href = "/login";
                    window.vue.$message({
                        message: "登录已过期，请重新登录",
                        type: 'warn'
                    });
                });
            } else {
                callback(result);
            }
        },
        error: function () {
            window.vue.$message({
                message: "请检查网络~",
                type: 'warn'
            });
        }
    });
}